<template>
  <el-container>
    <el-header>
      <div class="nav-bar">
        <div class="logo">后台管理系统</div>
        <div class="user-info">
          <el-avatar src="/api/placeholder/40/40" size="small"></el-avatar>
          <el-dropdown @command="handleCommand">
                        <span class="el-dropdown-link">
                            个人中心<i class="el-icon-arrow-down el-icon--right"></i>
                        </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">个人资料</el-dropdown-item>
                <el-dropdown-item command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </el-header>
    <el-container>
      <el-aside width="200px">
        <el-menu :default-active="activeMenu" mode="vertical">
          <el-menu-item v-for="item in menuItems" :key="item.path" :index="item.path" @click="handleMenuItemClick(item.path)">
            {{ item.label }}
          </el-menu-item>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { useStore } from 'vuex';

const store = useStore();
const route = useRoute();
const router = useRouter();
const menuItems = store.state.menuItems;
const activeMenu = ref(route.path);

const handleMenuItemClick = (path) => {
  activeMenu.value = path;
  router.push(path);
};

const handleCommand = (command) => {
  if (command === 'logout') {
    localStorage.removeItem('isLoggedIn');
    router.push('/login');
  } else if (command === 'profile') {
    // 可添加跳转到个人资料页面的逻辑
    console.log('跳转到个人资料页面');
  }
};
</script>

<style scoped>
.nav-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 60px;
  padding: 0 20px;
  background-color: #f0f2f5;
}

.logo {
  font-size: 20px;
  font-weight: bold;
}

.user-info {
  display: flex;
  align-items: center;
}

.el-avatar {
  margin-right: 10px;
}
</style>